package com.ishadows.notebook;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import com.activeandroid.Model;
import com.app.library.activity.BaseActivity;
import com.app.library.adapter.BaseModelAdapter;
import com.app.library.annotation.InjectView;
import com.ishadows.notebook.model.ScrapPaperModel;
import com.ishadows.notebook.model.TransitNoteModel;

public class NoteListActivity extends BaseActivity{
	private String dirName;
	@InjectView(ViewId=R.id.note_grid)
	private GridView noteGrid;
	private static int ADD_NOTE=0x1000;
	private static int CHANGE_NOTE=0x1001;
	private List<ScrapPaperModel> notes;
	private Long dirId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dirName =getIntent().getStringExtra("dirName");
		if(dirName==null){
			finish();
			return ;
		}
		setContentView(R.layout.main_note);
		injectView();
		noteGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				editNote(notes.get(position).getId());
				dirId=notes.get(position).getId();
			}
		});
		refreshNotes(dirName);
	}
	
	public void addNote(View v){
		editNote(-1);
	}
	
	public void deleteNote(View v){
		for(ScrapPaperModel note:notes){
			if(note.isCheck()){
				Model.delete(ScrapPaperModel.class, note.getId());
			}
		}
		refreshNotes(dirName);
	}
	
	public void refreshNotes(String dirName){
		notes =ScrapPaperModel.getAllNote(dirName);
		BaseModelAdapter adapter =new BaseModelAdapter(notes, this);
		noteGrid.setAdapter(adapter);
	}
	
	public void editNote(long id){
		
		Intent i =new Intent(this,EditNoteActivity.class);
		if(id==-1){
			i.putExtra("requestCode", ADD_NOTE);
			startActivityForResult(i, ADD_NOTE);
			return ;
		}
		ScrapPaperModel scrap =ScrapPaperModel.load(ScrapPaperModel.class, id);
		if(scrap!=null){
			i.putExtra("requestCode", CHANGE_NOTE);
			TransitNoteModel.getTransitNoteModel().setContent(scrap.getContent()).save();
		}
		startActivityForResult(i, CHANGE_NOTE);
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
	//	super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==CHANGE_NOTE){
			if(resultCode==Activity.RESULT_OK){
				ScrapPaperModel scrap  =ScrapPaperModel.load(ScrapPaperModel.class, dirId);
				TransitNoteModel tran =TransitNoteModel.getTransitNoteModel();
				scrap.setImgSmall(tran.getImg());
				scrap.setContent(tran.getContent());
				scrap.setDate(new Date());
				scrap.setDirName(dirName);
				scrap.save();
				refreshNotes(dirName);
			}
		}
		if(requestCode==ADD_NOTE){
			if(resultCode==Activity.RESULT_OK){
				ScrapPaperModel scrap  =new ScrapPaperModel();
				TransitNoteModel tran =TransitNoteModel.getTransitNoteModel();
				scrap.setImgSmall(tran.getImg());
				scrap.setContent(tran.getContent());
				scrap.setDate(new Date());
				scrap.setDirName(dirName);
				scrap.save();
				refreshNotes(dirName);
			}
		}
	}

}
